using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using AspNetForums.Components;

namespace AspNetForums.Admin
{
	/// <summary>
	/// Summary description for ManageCensorships.
	/// </summary>
	public class ManageCensorships : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataList dlManageCensors;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			dlManageCensors.EditCommand +=new DataListCommandEventHandler(dlManageCensors_EditCommand);
			dlManageCensors.CancelCommand +=new DataListCommandEventHandler(dlManageCensors_CancelCommand);
			dlManageCensors.DeleteCommand +=new DataListCommandEventHandler(dlManageCensors_DeleteCommand);
			dlManageCensors.UpdateCommand +=new DataListCommandEventHandler(dlManageCensors_UpdateCommand);

			dlManageCensors.DataSource = Censors.GetCensors();

			if( !Page.IsPostBack)
				dlManageCensors.DataBind();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void dlManageCensors_EditCommand(object source, DataListCommandEventArgs e) {
			dlManageCensors.EditItemIndex = e.Item.ItemIndex;
			dlManageCensors.DataBind();
		}

		private void dlManageCensors_DeleteCommand(object source, DataListCommandEventArgs e) {
			Censor censor = (Censor)((ArrayList)dlManageCensors.DataSource)[e.Item.ItemIndex];

			Censors.DeleteCensor( censor );
			dlManageCensors.EditItemIndex = -1;
			dlManageCensors.DataBind();
		}

		private void dlManageCensors_UpdateCommand(object source, DataListCommandEventArgs e) {
			TextBox txtWord = (TextBox)e.Item.FindControl("txtCensorWord");
			TextBox txtReplacement = (TextBox)e.Item.FindControl("txtCensorReplacement");

			if( txtWord == null ||
				txtReplacement == null )
				return;

			Censor censor = (Censor)((ArrayList)dlManageCensors.DataSource)[e.Item.ItemIndex];

			censor.Word = txtWord.Text;
			censor.Replacement = txtReplacement.Text;

			Censors.UpdateCensor( censor );
			dlManageCensors.EditItemIndex = -1;
			dlManageCensors.DataBind();
		}

		private void dlManageCensors_CancelCommand(object source, DataListCommandEventArgs e) {
			dlManageCensors.EditItemIndex = -1;
			dlManageCensors.DataBind();
		}
	}
}
